package com.beaut.project.service;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beaut.project.dao.CRMMapper;
import com.beaut.project.dao.GDMapper;
import com.beaut.project.dao.WareHouseMapper;
import com.beaut.project.util.StaticData;
import com.beaut.project.util.Utility;

@Transactional(rollbackFor = Exception.class)
@Service("warehouseService")
public class WareHouseService {
	@Autowired
	private WareHouseMapper warehouseMapper;
	@Autowired
	private CRMMapper crmMapper;
	@Autowired
	private GDMapper gdMapper;
	
	public String listPageWareHouse(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageWareHouse(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageWareHouseType(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageWareHouseType(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listWareHouseType(Map<String,Object> map){
		return Utility.lstMaptoJson(warehouseMapper.listWareHouseType(map));
	}
	public String listPageInventory(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageInventory(map);
		String warehouseNames="";
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
			String[]arrWare=String.valueOf(list.get(i).get("warehouse_id")).split(",");
			for(int j=0;j<arrWare.length;j++){
				map.put("id",arrWare[j] );
				Map<String,Object>mapWare=warehouseMapper.getWareHouseById(map);
				if(arrWare.length-1==j){
					warehouseNames+=mapWare.get("name");
				}else{
					warehouseNames+=mapWare.get("name")+",";
					
				}
				
			}
			list.get(i).put("warehouse_name",warehouseNames );
		
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listMaterialDetail(Map<String,Object> map){
		StringBuffer sb = new StringBuffer("");
		List<String> list = Arrays.asList(String.valueOf(map.get("warehouse_id")).split(","));
		List<String> listCust = Arrays.asList(String.valueOf(map.get("inventory_obj")).split(","));
		if(map.get("warehouse_id") != null){
			map.put("list", list);
			if(map.get("inventory_obj") != null){
				map.put("listCust", listCust);
			}else{
				map.put("type", "100");
			}
			List<Map<String,Object>> list1= warehouseMapper.listMaterialDetail(map);
			sb.append(Utility.lstMaptoJson(list1));
		}
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listWareHouse(Map<String,Object> map){
		StringBuffer sb = new StringBuffer("");
		List<String> list = Arrays.asList(String.valueOf(map.get("warehouse_id")).split(","));
		if(list.size()>0){
			List<Map<String,Object>> list1= warehouseMapper.listWareHouse(list);
			sb.append(Utility.lstMaptoJson(list1));
		}
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listInventoryDetail(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listInventoryDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getWareHouseById(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getWareHouseById(map));
	}
	public String getGdSupplierPurchase(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getGdSupplierPurchase(map));
	}
	public String getInventoryById(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getInventoryById(map));
	}
	public String getGDBaseById(Map<String,Object> map){
		return Utility.toJson(gdMapper.getGDBaseById(map));
	}
	public String getGdPurchase(Map<String,Object> map){
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", Utility.toJson(warehouseMapper.getGdPurchase(map)));
	}
	public String addInventory(Map<String,Object> map){
		String inventory_id = UUID.randomUUID().toString();
		map.put("inventory_id", inventory_id);
		map.put("flag", map.get("flag"));
		map.put("status", StaticData.NEW_CREATE);
		warehouseMapper.addInventory(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("inventory_id", inventory_id);
				if("1".equals(String.valueOf( m.get("type")))){	//甲供库	
					m.put("business_id",String.valueOf(m.get("project_id")));
				}else if("4".equals(String.valueOf( m.get("type")))){//厂家库
					m.put("business_id", String.valueOf(m.get("supplier_id")));
				}else if("0".equals(String.valueOf( m.get("type")))){		//客户库	
					m.put("business_id", String.valueOf(m.get("customer_id")));
				}else if("3".equals(String.valueOf( m.get("type")))){		//订单料	
					m.put("business_id", String.valueOf(m.get("purchase_id")));
				}else{//工余料
					m.put("business_id", "");
				}
				
				BigDecimal db_num = new BigDecimal(String.valueOf(m.get("db_num")));
				BigDecimal actually_num = new BigDecimal(String.valueOf(m.get("actually_num")));
				m.put("different_num", String.valueOf(db_num.subtract(actually_num)));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addInventoryDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String deleteInventory(Map<String,Object>map){
		warehouseMapper.deleteInventory(map);
		warehouseMapper.deleteInventoryDetail(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String updateInventory(Map<String,Object> map){
		map.put("status", StaticData.NEW_CREATE);
		warehouseMapper.updateInventory(map);
		warehouseMapper.deleteInventoryDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				BigDecimal db_num = new BigDecimal(String.valueOf(m.get("db_num")));
				BigDecimal different_num = new BigDecimal(0);
				BigDecimal actually_num = new BigDecimal(String.valueOf(m.get("actually_num")));
				m.put("different_num", String.valueOf(db_num.subtract(actually_num)));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addInventoryDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addWareHouse(Map<String,Object> map){
		warehouseMapper.addWareHouse(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateWareHouse(Map<String,Object> map){
		warehouseMapper.updateWareHouse(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteWareHouse(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		warehouseMapper.deleteWareHouse(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String listPageMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageJGMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageJGMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageMaterialWithSelfCost(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageMaterialWithSelfCost(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getCSMaterialByCsId(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.getCSMaterialByCsId(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getMaterialById(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getMaterialById(map));
	}
	public String getCustomerMaterialById(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getCustomerMaterialById(map));
	}
	public String addMaterial(Map<String,Object> map){
		warehouseMapper.addMaterial(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateMaterial(Map<String,Object> map){
		warehouseMapper.updateMaterial(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String addCustomerMaterial(Map<String,Object> map){
		String id = UUID.randomUUID().toString();
		map.put("c_s_id", id);
		map.put("status", StaticData.NEW_CREATE);
		if("1".equals(String.valueOf(map.get("flagType")))){
			map.put("summary", "厂家库产品录入【"+getSupplierName(String.valueOf(map.get("supplier_id")))+"】");
		}else{
			map.put("summary", "客户库产品录入【"+getCustomerName(String.valueOf(map.get("customer_id")))+"】");
		}
		map.put("status", StaticData.NEW_CREATE);
		warehouseMapper.addCustomerMaterial(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				String temp_id = UUID.randomUUID().toString();
				m.put("c_s_id", id);
				m.put("temp_id", temp_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addCustomerMaterialDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	
	public String updateCustomerMaterial(Map<String,Object> map){
		warehouseMapper.updateCustomerMaterial(map);
		
		warehouseMapper.deleteCustomerMaterialDetail(map);
		
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				String temp_id = UUID.randomUUID().toString();
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("c_s_id", String.valueOf(map.get("c_s_id")));
				m.put("temp_id", temp_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addCustomerMaterialDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteCusMaterialDetail(Map<String,Object> map){
		warehouseMapper.deleteCustMaterial(map);
		warehouseMapper.deleteCustomerMaterialDetail(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteMaterial(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		warehouseMapper.deleteMaterial(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String listPageMaterialDB(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageMaterialDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageSelfMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageSelfMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageJgMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageJgMaterial(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageJgDB(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageJgDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listCustomerPageMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listCustomerPageMaterial(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageCustomerMaterial(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageCustomerMaterial(map);
		for(int i=0;i<list.size();i++){
			Map<String,Object>mapCust=warehouseMapper.getCustById(map);
			list.get(i).put("customer", mapCust.get("company"));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listCustomerMaterialDetail(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listCustomerMaterialDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listDBProductNumber(Map<String,Object> map){
		return Utility.lstMaptoJson(warehouseMapper.listDBProductNumber(map));
	}
	public String checkSame(Map<String, Object> map) {
		if(warehouseMapper.checkSame(map) > 0){
			return Utility.rtJson(Utility.RETURN_ERROR, null, null);
		}
		return Utility.rtJson(Utility.RETURN_OK, null, null);
	}
	public String addBeforeMaterialDB(Map<String,Object> map){
		String warehouse_id = String.valueOf(map.get("warehouse_id"));
		String customer_id = String.valueOf(map.get("customer_id"));
    	String products = String.valueOf(map.get("products"));
    	String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
    	String[] productArr=products.split(";");
    	List<Map<String,Object>> dbList=new ArrayList<Map<String,Object>>();
    	for(int i=0;i<productArr.length;i++){
    		Map<String,String> productMap= Utility.json2Map(productArr[i]);
    		Map<String,Object> m= new HashMap<String,Object>();
    		m.put("warehouse_id", warehouse_id);
    		m.put("customer_id", customer_id);
    		m.put("project_id", "");
    		m.put("gd_id", "");
    		m.put("supplier_id", "");
    		m.put("product_id", productMap.get("id"));
    		m.put("cost", 0);//不计价格
    		m.put("num", productMap.get("num"));
    		m.put("top", productMap.get("top"));
    		m.put("low", productMap.get("low"));
    		m.put("person_id", map.get("person_id"));
    		m.put("person", map.get("person"));
    		m.put("operation_date", operation_date);
    		m.put("direction", StaticData.DB_IN);
    		m.put("business_type", StaticData.DB_INIT);
    		m.put("business_id", "");
    		m.put("summary", "期初入库【"+getCustomerName(customer_id)+"】");
    		dbList.add(m);
    	}
    	warehouseMapper.addBeforeMaterialDB(dbList);
    	warehouseMapper.addDBLog(dbList);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	private String getCustomerName(String customer_id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", customer_id);
		return String.valueOf(crmMapper.getCustomerById(map).get("company"));
	}
	private String getSupplierName(String supplier_id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", supplier_id);
		return String.valueOf(crmMapper.getSupplierById(map).get("company"));
	}
	public String addSupplierMaterialDB(Map<String,Object> map){
		String warehouse_id = String.valueOf(map.get("warehouse_id"));
		String supplier_id = String.valueOf(map.get("supplier_id"));
		String products = String.valueOf(map.get("products"));
		String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
		String[] productArr=products.split(";");
		List<Map<String,Object>> dbList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<productArr.length;i++){
			Map<String,String> productMap= Utility.json2Map(productArr[i]);
			Map<String,Object> m= new HashMap<String,Object>();
			m.put("warehouse_id", warehouse_id);
			m.put("supplier_id", supplier_id);
			m.put("customer_id", "");
    		m.put("project_id", "");
    		m.put("gd_id", "");
			m.put("product_id", productMap.get("id"));
			m.put("cost", 0);//不计价格
			m.put("num", productMap.get("num"));
			m.put("top", productMap.get("top"));
			m.put("low", productMap.get("low"));
			m.put("person_id", map.get("person_id"));
    		m.put("person", map.get("person"));
			m.put("operation_date", operation_date);
			m.put("direction", StaticData.DB_IN);
			m.put("business_type", StaticData.DB_INIT);
			m.put("business_id", "");
			m.put("summary", "期初入库【"+getSupplierName(supplier_id)+"】");
			dbList.add(m);
		}
		warehouseMapper.addSupplierMaterialDB(dbList);
		warehouseMapper.addDBLog(dbList);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addMyMaterialDB(Map<String,Object> map){
		String warehouse_id = String.valueOf(map.get("warehouse_id"));
		String products = String.valueOf(map.get("products"));
		String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
		String[] productArr=products.split(";");
		List<Map<String,Object>> dbList=new ArrayList<Map<String,Object>>();
		for(int i=0;i<productArr.length;i++){
			Map<String,String> productMap= Utility.json2Map(productArr[i]);
			Map<String,Object> m= new HashMap<String,Object>();
			m.put("warehouse_id", warehouse_id);
			m.put("product_id", productMap.get("id"));
			m.put("supplier_id", "");
			m.put("customer_id", "");
			m.put("project_id", "");
			m.put("gd_id", "");
			//获取该产品所有的库存数量和单价
			Map<String,Object> productDBMap = warehouseMapper.getSelfProductDB(m);
			if(productDBMap == null){
				m.put("cost", productMap.get("cost"));
			}else{
				//加权平均
				BigDecimal oldCost = new BigDecimal(String.valueOf(productDBMap.get("cost")));
				BigDecimal money = new BigDecimal(0);
				if(oldCost.compareTo(money) == 0){
					m.put("cost", m.get("cost"));
				}else{
					BigDecimal oldNum = new BigDecimal(String.valueOf(productDBMap.get("num")));
					BigDecimal num = new BigDecimal(String.valueOf(productMap.get("num")));
					BigDecimal cost = new BigDecimal(String.valueOf(productMap.get("cost")));
					BigDecimal newCost = oldNum.multiply(oldCost).add(num.multiply(cost)).divide(oldNum.add(num),2,BigDecimal.ROUND_HALF_DOWN);
					m.put("cost", newCost);
				}
				warehouseMapper.updateSelfCost(m);
			}
			m.put("num", productMap.get("num"));
			m.put("top", productMap.get("top"));
			m.put("low", productMap.get("low"));
			m.put("person_id", map.get("person_id"));
    		m.put("person", map.get("person"));
			m.put("operation_date", operation_date);
			m.put("direction", StaticData.DB_IN);
			m.put("business_type", StaticData.DB_INIT);
			m.put("business_id", "");
			m.put("summary", "期初入库");
			dbList.add(m);
		}
		warehouseMapper.addMyMaterialDB(dbList);
		warehouseMapper.addDBLog(dbList);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}

	public String listPageBeforeDBLog(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageBeforeDBLog(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageMyDBLog(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageMyDBLog(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPagePurchaseApplyDBLog(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPagePurchaseApplyDBLog(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageMyDBLog1(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageMyDBLog1(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageDBSupplierLog(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageDBSupplierLog(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	
	public String listPageWarehouMaterialDB(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageWarehouMaterialDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageBeforeDB(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageBeforeDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageMyDB(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageMyDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageSupplierDB(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageSupplierDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPagePurchaseApplyDB(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPagePurchaseApplyDB(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	
	public String listPageGDPurchaseApply(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageGDPurchaseApply(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageFinishGDPurchaseApply(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageFinishGDPurchaseApply(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listSupplierDb(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listSupplierDb(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listSupplierDbView(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listSupplierDbView(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getGDBaseInfoByApplyId(Map<String,Object> map){
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", Utility.toJson(warehouseMapper.getGDBaseInfoByApplyId(map)));
	}
	public String getSupplierDbById(Map<String,Object> map){
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", Utility.toJson(warehouseMapper.getSupplierDbById(map)));
	}
	public String getGDJgByApplyId(Map<String,Object> map){
		if("1".equals(String.valueOf(map.get("business_type")))){
			return Utility.rtJson(Utility.RETURN_OK, "操作成功", Utility.toJson(warehouseMapper.getGDJgByApplyId(map)));
		}else{
			return Utility.rtJson(Utility.RETURN_OK, "操作成功", Utility.toJson(warehouseMapper.getGDYgByApplyId(map)));
		}
	}
	public String getPurchaseById(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getPurchaseById(map));
	}
	public String getPurchaseProductInCount(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getPurchaseProductInCount(map));
	}
	public String getSupplierPurchaseById(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getSupplierPurchaseById(map));
	}
	public String addSupplierPurchase(Map<String,Object> map){
    	String operation_date = new SimpleDateFormat(StaticData.OPERATION_DATE_FORMATTER).format(new Date());
		Map<String,Object> gdBaseInfo = gdMapper.getGDContentByGDId(map);
		map.put("gd_code", gdBaseInfo.get("gd_code"));
		map.put("gd_name", gdBaseInfo.get("gd_name"));
		map.put("gd_admin_id", gdBaseInfo.get("gd_admin_id"));
		map.put("gd_admin_name", gdBaseInfo.get("gd_admin_name"));
		map.put("gd_dept_id", gdBaseInfo.get("dept_id"));
		map.put("gd_dept_name", gdBaseInfo.get("dept_name"));
		String purchase_id = UUID.randomUUID().toString();
		map.put("purchase_id", purchase_id);
		map.put("status", StaticData.NEW_CREATE);
		warehouseMapper.addSupplierPurchase(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		BigDecimal finance_out = new BigDecimal(String.valueOf(map.get("total_money")));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("purchase_id",purchase_id);
				proList.add(m);
				m.put("supplier_id",String.valueOf(map.get("supplier_id")));
				m.put("gd_id",String.valueOf(map.get("gd_id")));
				warehouseMapper.updateSupperPurchase(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addSupplierPurchaseDetail(proList);
		}
		//3.记供货商应付(按照本次入库单计算总额，不对每个材料进行添加，所以放在循环外面)
		Map<String,Object> fodMap = getSupplierFOD(String.valueOf(map.get("supplier_id")),String.valueOf(map.get("gd_id")));
		String detail_id = null;
		BigDecimal before_finance_out = new BigDecimal(0);
		if(fodMap == null){
			fodMap = new HashMap<String,Object>();
			detail_id = UUID.randomUUID().toString();
		    fodMap.put("id", detail_id);
			fodMap.put("supplier_id", map.get("supplier_id"));
			fodMap.put("gd_id", map.get("gd_id"));
			fodMap.put("finance_out", finance_out);
			crmMapper.addSupplierFOD(fodMap);
		}else{
			before_finance_out = new BigDecimal(String.valueOf(fodMap.get("finance_out")));
			fodMap.put("finance_out", finance_out);
			crmMapper.updateSupplierFOD(fodMap);
			detail_id = String.valueOf(fodMap.get("id"));
		}
	    
		crmMapper.updateSupplierFO(fodMap);
		Map<String,Object> fodLogMap = new HashMap<String,Object>();
		fodLogMap.put("detail_id", detail_id);
		fodLogMap.put("before_finance_out", before_finance_out);
		fodLogMap.put("supplier_id", map.get("supplier_id"));
		fodLogMap.put("supplier", fodMap.get("company"));
		fodLogMap.put("operation_date", operation_date);
		fodLogMap.put("proof_type", StaticData.CRM_SUPPLIER_BC_PRODUCT);
		fodLogMap.put("proof_id", purchase_id);
		fodLogMap.put("finance_out", finance_out);
		fodLogMap.put("after_finance_out", before_finance_out.add(finance_out));
		fodLogMap.put("summary", "工单【"+String.valueOf(gdBaseInfo.get("gd_code"))+"】补采购编号【"+String.valueOf(map.get("code"))+"】补采购【还厂家库】，应付增加");
		crmMapper.addSupplierFODLog(fodLogMap);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	private Map<String,Object> getSupplierFOD(String supplier_id,String gd_id){
		Map<String,Object>  queryMap = new HashMap<String,Object>();
		queryMap.put("supplier_id", supplier_id);
		queryMap.put("gd_id", gd_id);
		return crmMapper.getSupplierFOD(queryMap);
	}
	public String addPurchase(Map<String,Object> map){
		Map<String,Object> gdBaseInfo = warehouseMapper.getGDBaseInfoByApplyId(map);
		map.putAll(gdBaseInfo);
		String purchase_id = UUID.randomUUID().toString();
		map.put("purchase_id", purchase_id);
		map.put("status", StaticData.NEW_CREATE);
		warehouseMapper.addPurchase(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("purchase_id",purchase_id);
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addPurchaseDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updatePurchase(Map<String,Object> map){
		warehouseMapper.updatePurchase(map);
		warehouseMapper.deletePurchaseDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("purchase_id",String.valueOf(map.get("purchase_id")));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addPurchaseDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String deletePurchase(Map<String,Object> map){
		warehouseMapper.deletePurchase(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String listPagePurchase(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPagePurchase(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageFinishPurchase(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageFinishPurchase(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPurchaseDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(warehouseMapper.listPurchaseDetail(map));
	}
	public String addPurchaseProductIn(Map<String,Object> map){
		Map<String,Object> purchaseMap = warehouseMapper.getPurchaseById(map);
		String product_in_id = UUID.randomUUID().toString();
		map.put("product_in_id", product_in_id);
		map.put("gd_id", purchaseMap.get("gd_id"));
		map.put("gd_code", purchaseMap.get("gd_code"));
		map.put("gd_name", purchaseMap.get("gd_name"));
		map.put("gd_dept_id", purchaseMap.get("gd_dept_id"));
		map.put("supplier_id", purchaseMap.get("company1_id"));
		map.put("supplier", purchaseMap.get("company1"));
		map.put("status", StaticData.NEW_CREATE);
		warehouseMapper.addPurchaseProductIn(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("product_in_id",product_in_id);
				m.put("receive_num",m.get("inNum"));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addPurchaseProductInDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String listPagePurchaseProductIn(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPagePurchaseProductIn(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageFinishPurchaseProductIn(Map<String,Object> map){
		List<Map<String,Object>> list= warehouseMapper.listPageFinishPurchaseProductIn(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("status_desc", StaticData.ADUIT_STATUS.get(String.valueOf(list.get(i).get("status"))));
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPurchaseProductInDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(warehouseMapper.listPurchaseProductInDetail(map));
	}
	public String listSupplierPurchaseDetail(Map<String,Object> map){
		return Utility.lstMaptoJson(warehouseMapper.listSupplierPurchaseDetail(map));
	}
	public String getPurchaseProductInById(Map<String,Object> map){
		return Utility.toJson(warehouseMapper.getPurchaseProductInById(map));
	}
	public String updatePurchaseProductIn(Map<String,Object> map){
		warehouseMapper.updatePurchaseProductIn(map);
		warehouseMapper.deletePurchaseProductInDetail(map);
		List<Map<String,String>> proList=new ArrayList<Map<String,String>>();
		String strs = String.valueOf(map.get("products"));
		if(!Utility.isNullorEmpty(strs)){
			String[] strArr=strs.split(";");
			for(int i=0;i<strArr.length;i++){
				Map<String,String> m= Utility.json2Map(strArr[i]);
				m.put("product_in_id",String.valueOf(map.get("product_in_id")));
				m.put("receive_num",m.get("inNum"));
				proList.add(m);
			}
		}
		if(proList.size() > 0){
			warehouseMapper.addPurchaseProductInDetail(proList);
		}
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String deletePurchaseProductIn(Map<String,Object> map){
		warehouseMapper.deletePurchaseProductIn(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String listPageGDSupplierPurchase(Map<String,Object> map){
		List<Map<String,Object>> list= gdMapper.listPageGDSupplierApplyForPurchase(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
}
